import { MirrorDataFilter } from "@/modules/dev-env/mirror/filter.ts";
import type { DefaultOptionType } from "ant-design-vue/es/vc-select/Select";
import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import { parseDragonwellVersions } from "@/modules/dev-env/java/jdk/jdk-utils.ts";

class DragonwellJdkEditionFilter extends MirrorDataFilter<string> {
  constructor() {
    super("版本类型", "standard");
  }

  protected getDataSource0(): Promise<DefaultOptionType[]> {
    return Promise.resolve([
      { label: "标准版", value: "standard" },
      { label: "增强版", value: "extended" },
    ]);
  }

  filter0(versions: ToolInstallVersionItem[]): ToolInstallVersionItem[] {
    return versions.filter(it => it.edition === this.value);
  }
}

/**
 * 阿里巴巴龙井JDK下载源
 */
export class DragonwellJdkMirror extends BaseEnvMirror {
  constructor() {
    super({
      id: "openjdk.dragonwell",
      name: "Dragonwell",
      url: "https://dragonwell-jdk.io/releases.json",
      variant: "dragonwell",
      website: "https://dragonwell-jdk.io/",
    });
  }

  getFilters0(): MirrorDataFilter[] {
    return [new DragonwellJdkEditionFilter()];
  }

  async getDownloadList(): Promise<ToolInstallVersionItem[]> {
    try {
      const res = await native.http.getJson(this.url);
      const standardList = parseDragonwellVersions(res.data.oss.standard, "standard");
      const extendedList = parseDragonwellVersions(res.data.oss.extended, "extended");
      return [...standardList, ...extendedList];
    } catch (e: any) {
      console.error("getDragonwellJdkDownloadList error", e);
      return [];
    }
  }
}
